function refreshAssetChange(Store,URL){
	Ext.getStore(Store).getProxy().url = URL;
    Ext.getStore(Store).load();	
}

function searchAssetChange(Store,URL,keyword){
	Ext.getStore(Store).getProxy().url = URL+keyword;
    Ext.getStore(Store).load();
}


function getValueGridRelocation(record){
	Ext.getCmp('ButtonAddCategory').setText('Update');
	var id  = record.get('CATEGORY_ID');
	if(Ext.getCmp('ConfigAssetCategory').hidden == true) Ext.getCmp('ConfigAssetCategory').toggleCollapse(true);
	Ext.Ajax.request({
	    method: "GET",
	    dataType: "json",
	    url: "./Services/DataMaster/AssetCategory?CATEGORY="+id+"",
	    async: false	,
	    success: function(c) {
	    	var jsonResults= $.parseJSON(c.responseText);
	    	data = jsonResults.item[0];
	    	Ext.getCmp('ConfigAssetCategory').getForm().setValues(data);
	        }
	    });	
}


function ShowGridAsset(id_panel){
	var hidden = true;
	if  (id_panel=="assetrelocationne" || id_panel == "assethentioperasiNe" || id_panel == "assetdismantlene" || id_panel == "assethilangsite"){
		hidden = false;
	}else{
		hidden = true;
		
	}
	   Ext.getCmp(id_panel).removeAll();
	   Ext.getCmp(id_panel).add(
       Ext.create('Ext.grid.Panel', {
   	   scroll: 'both',
 	     selType: 'checkboxmodel',
 	     columnLines: true,
 	     //hidden : true,
 	     id     : 'tabletemp'+id_panel,
 	     title : 'List Of Asset',
 	     width: 587,
 	     height: 250,
 	     titleAlign: 'center',
 	     store: 'AssetDataMaster.AssetNonSac',
 	     margin: '0 5 0 5',
 	     border: true,
 	     columns: [
					 {dataIndex: 'ASSET_DETAIL_ID', text: ' ASSET DETAIL ID', align: 'left'},
					 { dataIndex: 'SITE_ID', text: 'SITE ID', align: 'left' },
					 {dataIndex: 'CATEGORY', text: 'CATEGORY', align: 'left'},
					 {dataIndex: 'SUB_CATEGORY', text: 'SUB CATEGORY', align: 'left'},
					 {dataIndex: 'BRAND', text: 'BRAND', align: 'left'},
					 {dataIndex: 'TYPE', text: 'TYPE', align: 'left'},
					 {dataIndex: 'CAPACITY_UNIT', text: 'CAPCITY UNIT', align: 'left'},
					 {dataIndex: 'CAPACITY_QTY', text: 'CAPACITY QTY', align: 'left'},
					 {dataIndex: 'ITEM_UNIT', text: 'ITEM UNIT', align: 'left'},
					 {dataIndex: 'ITEM_QTY', text: 'ITEM QTY', align: 'left'},
					 {dataIndex: 'SKU_NUMBER', text: 'SKU NUMBER', align: 'left'},
					 {dataIndex: 'PART_NUMBER', text: 'PART NUMBER', align: 'left'},
					 {dataIndex: 'SERIAL_NUMBER', text: 'SERIAL NUMBER', align: 'left'},
					 {dataIndex: 'STATUS', text: 'STATUS', align: 'left'},
					 {dataIndex: 'REFERENCE', text: 'REFERENCE', align: 'left'},
					 {dataIndex: 'PO_NUMBER', text: 'PO NUMBER', align: 'left'},
					 {dataIndex: 'RCV_NUMBER', text: 'RCV NUMBER', align: 'left'},
					 {dataIndex: 'BAST', text: 'BAST', align: 'left'}],
 	     dockedItems: [
 	        {
 	            xtype: 'toolbar',
 	            dock: 'top',
 	            items: [
                   {
                    	xtype:'panel',
                    	width:450,
                    	border:false
                    	
                    },
 	                {
 	                	  xtype: 'button',
 	                	  text: 'DELETE',
 	                	  margin: '0 20 0 0',
 	                	  handler: function () {
 	                		 var rec = Ext.getCmp('tabletemp'+id_panel).getSelectionModel().getSelection();
			  	              	if (rec.length>0){
			  	              		        	 for (var i = 0; i<rec.length ; i++){
			  	              		        		var data = {ASSET_DETAIL_ID:rec[i].get('ASSET_DETAIL_ID')};
			  	              		        		var URL  = './Services/AssetChange/Undo/Change/Update';
			  	              		     	    	PostData(data,URL,"Undo Change");
			  	              		     	    }
			  	              	}else{
			  	                  	Ext.MessageBox.show({
			  	              		 	title: 'WARNING!',
			  	              	        msg: 'No Data Selected',
			  	              	        icon: Ext.Msg.WARNING
			  	              	    });	
			  	                  }
 	 	                    }
 	                },
 	                {
 	                	  xtype: 'button',
	                	  text: 'EXPORT',
	                	  margin: '0 30 0 0',
	                	  handler: function () {
	                		  location.href = "./Services/AssetChange/Report/Asset/Change/Pdf?change_id="+change_id_global;
	 	                    }	
 	                }
 	            ]
 	        }
 	    ],
          
          }));

}

function openPopupNE() {
	  var win = new Ext.Window({
	        modal : true,
	        height : 0.4 * screen.width,
	        width : 1080,
	        border : false,
	        resizable : false,
	        closable : true,
	        title : 'SELECT SITE TO CHANGE',
	        layout : 'fit',
	        titleAlign : 'center',
	        store : 'AssetDataMaster.SiteList',
	        frame : true,
	        autoScroll : true,
	        items : [               {
   	   	     xtype : 'gridpanel',
	   	   	 scroll: 'both',
	 	     selType: 'checkboxmodel',
	 	     columnLines: true,
	 	     width: 587,
	 	     height: 250,
	 	     titleAlign: 'center',
	 	     store: 'AssetDataMaster.AssetNonSac',
	 	     margin: '0 5 0 5',
	 	     border: true,
	 	     columns: [
	     	        {dataIndex: 'ASSET_DETAIL_ID', text: ' ASSET DETAIL ID', align: 'left'},
	                { dataIndex: 'SITE_ID', text: 'SITE ID', align: 'left' },
	     	        {dataIndex: 'CATEGORY', text: 'CATEGORY', align: 'left'},
	     	        {dataIndex: 'SUB_CATEGORY', text: 'SUB CATEGORY', align: 'left'},
	     	        {dataIndex: 'BRAND', text: 'BRAND', align: 'left'},
	     	        {dataIndex: 'TYPE', text: 'TYPE', align: 'left'},
	     	        {dataIndex: 'CAPACITY_UNIT', text: 'CAPCITY UNIT', align: 'left'},
	     	        {dataIndex: 'CAPACITY_QTY', text: 'CAPACITY QTY', align: 'left'},
	     	        {dataIndex: 'ITEM_UNIT', text: 'ITEM UNIT', align: 'left'},
	     	        {dataIndex: 'ITEM_QTY', text: 'ITEM QTY', align: 'left'},
	     	        {dataIndex: 'SKU_NUMBER', text: 'SKU NUMBER', align: 'left'},
	     	        {dataIndex: 'PART_NUMBER', text: 'PART NUMBER', align: 'left'},
	     	        {dataIndex: 'SERIAL_NUMBER', text: 'SERIAL NUMBER', align: 'left'},
	     	        {dataIndex: 'STATUS', text: 'STATUS', align: 'left'},
	     	        {dataIndex: 'REFERENCE', text: 'REFERENCE', align: 'left'},
	     	        {dataIndex: 'PO_NUMBER', text: 'PO NUMBER', align: 'left'},
	     	        {dataIndex: 'RCV_NUMBER', text: 'RCV NUMBER', align: 'left'},
	     	        {dataIndex: 'BAST', text: 'BAST', align: 'left'}],
	     	       listeners : {
	                   itemdblclick : function(grid, record) {
	                	   Ext.getStore("AssetChange.TempStore").add(record.data);
	                   	        	
	   	                }
	                 },
	 	     dockedItems: [
	 	        {
	 	            xtype: 'toolbar',
	 	            dock: 'top',
	 	            items: [
	 	               
						{
						    xtype : 'combobox',
						    labelWidth : 90,
						    fieldLabel : 'Category',
						    displayField : 'area_description',
						    emptyText : 'All Area',
						    width : 200,
						    //id : 'comboAreaDismantleSite',
						    store : 'AssetDataMaster.Area',
						    margin : '0 5 0 5',
						    listeners : {
						      select : function(obj, newValue, oldValue, eOpts) {
						        var idArea = Ext.getCmp('comboAreaDismantleSite').getValue();
						        Ext.getStore('AssetDataMaster.Area').removeAll();
						        Ext.getStore('AssetDataMaster.Area').getProxy().url = './Services/DataMaster/Area/List?area=';
						        Ext.getStore('AssetDataMaster.Area').load();
						      }
						    }
						}
	 	            ]
	 	        }
	 	    ]}]
	      }).show();
	}

function return_AssetChange(form, action,id_panel){
	  var respon = Ext.JSON.decode(action.response.responseText);
	      for (var i=0;i<respon.item.length;i++){
	    	 Ext.Msg.alert('Submited', respon.item[i].Status);
	    	  if (respon.item[i].Status=="Success"){
	    	  change_id_global = respon.item[i].ChangeID;
//              Ext.getStore('AssetDataMaster.AssetNonSac').getProxy().url = './Services/AssetChange/Change/Details/List/'+respon.item[i].ChangeID+'?start=0&limit=20';
//              Ext.getStore('AssetDataMaster.AssetNonSac').load();
//              ShowGridAsset(id_panel);  
	    	  }
	    }
}

function tempDataNE(id_panel,store){
	
	var hidden = true;
	if  (id_panel=="assetrelocationne" || id_panel == "assethentioperasiNe" || id_panel == "assetdismantlene" || id_panel == "assethilangsite"){
		hidden = false;
		Ext.getStore("AssetChange.TempStore").removeAll();
	}else{
		hidden = true;
		
	}
	   Ext.getCmp(id_panel).removeAll();
	   Ext.getCmp(id_panel).add(
			 Ext.create('Ext.grid.Panel', {
	   	   	 scroll: 'both',
	 	     columnLines: true,
	 	     //hidden : true,
	 	     id     : 'tabletemp'+id_panel,
	 	     title : 'List of Asset to Process',
	 	     width: 587,
	 	     height: 250,
	 	     titleAlign: 'center',
	 	     store: store,
	 	     margin: '0 5 0 5',
	 	     border: true,
	 	     columns: [
						 {dataIndex: 'ASSET_DETAIL_ID', text: ' ASSET DETAIL ID', align: 'left'},
						 { dataIndex: 'SITE_ID', text: 'SITE ID', align: 'left' },
						 {dataIndex: 'CATEGORY', text: 'CATEGORY', align: 'left'},
						 {dataIndex: 'SUB_CATEGORY', text: 'SUB CATEGORY', align: 'left'},
						 {dataIndex: 'BRAND', text: 'BRAND', align: 'left'},
						 {dataIndex: 'TYPE', text: 'TYPE', align: 'left'},
						 {dataIndex: 'CAPACITY_UNIT', text: 'CAPCITY UNIT', align: 'left'},
						 {dataIndex: 'CAPACITY_QTY', text: 'CAPACITY QTY', align: 'left'},
						 {dataIndex: 'ITEM_UNIT', text: 'ITEM UNIT', align: 'left'},
						 {dataIndex: 'ITEM_QTY', text: 'ITEM QTY', align: 'left'},
						 {dataIndex: 'SKU_NUMBER', text: 'SKU NUMBER', align: 'left'},
						 {dataIndex: 'PART_NUMBER', text: 'PART NUMBER', align: 'left'},
						 {dataIndex: 'SERIAL_NUMBER', text: 'SERIAL NUMBER', align: 'left'},
						 {dataIndex: 'STATUS', text: 'STATUS', align: 'left'},
						 {dataIndex: 'REFERENCE', text: 'REFERENCE', align: 'left'},
						 {dataIndex: 'PO_NUMBER', text: 'PO NUMBER', align: 'left'},
						 {dataIndex: 'RCV_NUMBER', text: 'RCV NUMBER', align: 'left'},
						 {dataIndex: 'BAST', text: 'BAST', align: 'left'}],
	 	     dockedItems: [
	 	        {
	 	            xtype: 'toolbar',
	 	            dock: 'top',
	 	            items: [
 	                    {
 	                    	xtype:'panel',
 	                    	width:470,
 	                    	border:false
 	                    	
 	                    },
	 	                {
	 	                	xtype: 'button',
	  	                    text: 'APPLY',
	  	                    margin: '0 20 0 0',
	  	                    handler: function () {
	  	                    	    var url = "";
	  	                    	    var site_id_to = "";
	  	                    	    var category= "";
	  	                    	    var ASSET_DETAIL_ID = "";
	  	                    	    var site_id_from = "";  
	  	                    	    var url_add_Detail="./Services/AssetChange/Details/Add";
	  	                    	    if (id_panel=="assetrelocationne"){
	  	                    	    	url = './Services/AssetChange/Site/NE/Update';
	  	                    	    	site_id_to =Ext.getCmp("relocationneidto").getValue();
	  	                    	    	site_id_from =Ext.getCmp("relocationneid").getValue(); 
	  	                    	    }else if (id_panel=="assethentioperasiNe"){
	  	                    	    	url = './Services/AssetChange/Status/Henti/Operasi/Update';
	  	                    	    	site_id_from =Ext.getCmp("hentioperasineid").getValue(); 
	  	                    	    	site_id_to =""; 
	  	                    	    	
	  	                    	    }else if (id_panel=="assetdismantlene"){
	  	                    	    	url = './Services/AssetChange/Status/Dismantle/Update';
	  	                    	    	site_id_to ="";
	  	                    	    	site_id_from =Ext.getCmp("siteIdtDismantleNE").getValue();
	  	                    	    	
	  	                    	    }else if (id_panel=="assethilangsite"){
	  	                    	    	url = './Services/AssetChange/Status/Hilang/Update';
	  	                    	    	site_id_to ="";
	  	                    	    	site_id_from =Ext.getCmp("assethilangid").getValue();
	  	                    	    	
	  	                    	    }else if (id_panel=="assetrelocationsite"){
	  	                    	    	url = './Services/AssetChange/Relocation/Site/Update';
	  	                    	    	site_id_to = Ext.getCmp("relocationsiteidto").getValue();
	  	                    	    	site_id_from =Ext.getCmp("relocationsiteid").getValue();
	  	                    	    	
	  	                    	    }else if (id_panel=="assethentioperasisite"){
	  	                    	    	url = './Services/AssetChange/Site/Status/Henti/Operasi/Update';
	  	                    	    	site_id_to = "";
	  	                    	    	site_id_from = Ext.getCmp("hentioperasisiteid").getValue();
	  	                    	    	
	  	                    	    }else if (id_panel=="assetdismantlesite"){
	  	                    	    	url = './Services/AssetChange//Site/Status/Dismantle/Update';
	  	                    	    	site_id_to = "";
	  	                    	    	site_id_from =Ext.getCmp("textSiteIdtDismantleSite").getValue();
	  	                    	    	
	  	                    	    }
	  	                    	    
	  	                    	    
			  	                  	var rec = Ext.getStore(store).getRange();
			  	                  	for (var i =0; i<rec.length; i++){
			  	                  	if (i==0){
			  	                  	ASSET_DETAIL_ID+=rec[i].get('ASSET_DETAIL_ID');	
			  	                    category+=rec[i].get('CATEGORY');
			  	                  	}else{
			  	                  	ASSET_DETAIL_ID+="-"+rec[i].get('ASSET_DETAIL_ID');	
			  	                  	category+="-"+rec[i].get('CATEGORY');
			  	                  	}
			  	                  	}
			  	                  	console.log (ASSET_DETAIL_ID);
			  	                  	
				  	              	if (rec.length>0){
				  	              		
				  	              		if (id_panel=="assetrelocationsite" || id_panel=="assethentioperasisite" || id_panel=="assetdismantlesite" ){
		  	              		     	    	var data = {CHANGE_ID:change_id_global, SOURCE_SITE_ID :site_id_from, ASSET_DETAIL_ID :ASSET_DETAIL_ID};
		  	              		     	    	PostData(data,url_add_Detail,"Insert Detail");
		  	              		     	    	
			  		              			    var data = {FROM_SITE_ID:site_id_from, TO_SITE_ID:site_id_to};
			  			                        PostData(data,url,"Asset Change Update");
		  	              		     	    	
		  	              		     	    
				  	              		}else{
		  	              		     	    	var data = {CHANGE_ID:change_id_global, SOURCE_SITE_ID :site_id_from, ASSET_DETAIL_ID :ASSET_DETAIL_ID};
		  	              		     	    	PostData(data,url_add_Detail,"INSERT DETAIL");
				  	              	
		  	              		     	    	var data = {CATEGORY:category, SITE_ID :site_id_to, ASSET_DETAIL_ID :ASSET_DETAIL_ID};
		  	              		     	    	PostData(data,url,"Asset Change Update");
		  	              		     	    
				  	              		}
				  	              		        	
				  	              	}else{
				  	                  	Ext.MessageBox.show({
				  	              		 	title: 'WARNING!',
				  	              	        msg: 'No Data Selected',
				  	              	        icon: Ext.Msg.WARNING
				  	              	    });	
				  	                  }
	  	                    }
	 	                	
	 	                	
	 	                },
	 	                {
	 	                	
	 	                	  xtype: 'button',
		                	  text: 'ADD',
		                	  hidden :hidden,
		                	  margin: '0 30 0 0',
		                	  handler: function () {
		                		  Ext.getStore('AssetDataMaster.AssetNonSac').getProxy().url = './Services/AssetChange/Asset/From/Site/List?site_id='+site_id_ne;
                                  Ext.getStore('AssetDataMaster.AssetNonSac').load(); 
		                		  openPopupNE();
		 	                    }	
	 	                	
	 	                }
	 	            ]
	 	        }
	 	    ],
			 }));
	
}



function openFIle(grid,rowIndex){
	 var rec = grid.getStore().getAt(rowIndex);
  	 var typefile=rec.get('document').split(".");
  	 if (typefile[1]=="pdf" || typefile[1]=="xlsx" || typefile[1]=="docx" ){
  		window.open('./Services/AssetChange/doc/'+typefile[1]+'/'+rec.get('document')); 
  	 }else{
  		 window.open('./Services/AssetChange/doc/img/'+rec.get('document'));  
  	 }
}
